package com.dwxt.cdms.service;

import com.dwxt.cdms.admin.service.UserServiceAdmin;
import com.dwxt.cdms.api.common.utils.DateUtil;
import com.dwxt.cdms.applets.dto.IndexGraphDayResDTO;
import com.dwxt.cdms.applets.dto.IndexGraphResDTO;
import com.dwxt.cdms.applets.dto.IndexReqDTO;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.HeartVoiceMapper;
import com.dwxt.cdms.entity.*;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.PartyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class HeartVoiceService {

    @Autowired
    private HeartVoiceMapper heartVoiceMapper;
    @Autowired
    private EquipmentCustomService equipmentCustomService;
    @Autowired
    private HealthService healthService;
    @Autowired
    private StationService stationService;


    /**
     * 插入设备采集数据
     *
     * @param heartVoice
     * @return
     */
    public BaseResult insertData(HeartVoice heartVoice) {
        try {
            heartVoice.setUserId(PartyUtil.getCurrentUserId());
            heartVoice.setCategory("app");
        } catch (Exception e) {
            log.error("小程序上传心之声" + DateUtil.parseStr(new Date(), DateUtil.FMT_YYYYMMDD_HHMMSS));
            heartVoice.setCategory("small");
        }
        String deviceId = heartVoice.getDeviceId();
        EquipmentCustom equipmentCustom = equipmentCustomService.selectEquipmentCustomByDeviceIdentity(deviceId);
        if (equipmentCustom != null && equipmentCustom.getCustomerId() != null) {
            heartVoice.setCustomerId(equipmentCustom.getCustomerId());
            healthService.judgeIsNormalAndInsertRecord(heartVoice);
        }
        int i = heartVoiceMapper.insert(heartVoice);
        if (i != 1) {
            return BaseResult.build(StatusCode.HEARTVOICE_INSERT_FAIL);
        } else {
            return BaseResult.ok();
        }
    }

    public PageInfo<HeartVoice> selectPersonData(Integer pageNum, Integer pageSize, String userId) {
        PageHelper.startPage(pageNum, pageSize);
        HeartVoiceExample heartVoiceExample = new HeartVoiceExample();
        if (StringUtils.isNotEmpty(userId)) {
            heartVoiceExample.createCriteria().andUserIdEqualTo(userId);
        }
        heartVoiceExample.setOrderByClause("`create_time` DESC");
        List<HeartVoice> heartVoices = heartVoiceMapper.selectByExample(heartVoiceExample);
        return new PageInfo<HeartVoice>(heartVoices);
    }

    public BaseResult selectHeartVoicePage(Page page, Customer customer, Integer isAbnormal, String start, String end, String customerId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Map> maps = heartVoiceMapper.selectHeartVoicePage(customer, isAbnormal, start, end, customerId);
        PageInfo<Map> pageInfo = new PageInfo<>(maps);
        return BaseResult.ok(pageInfo);
    }

    public BaseResult insert(HeartVoice heartVoice) {
        heartVoiceMapper.insertSelective(heartVoice);
        return BaseResult.ok();
    }

    public BaseResult update(HeartVoice heartVoice) {
        heartVoiceMapper.updateByPrimaryKeySelective(heartVoice);
        return BaseResult.ok();
    }

    public BaseResult delete(Integer id) {
        heartVoiceMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }

    /**
     * 批量删除
     */
    public void deleteByIds(Integer[] ids) {
        int i = 0;
        if (ids.length > 0) {
            i = heartVoiceMapper.deleteByIds(ids);
        }
    }

    /**
     * 查询
     */
    public BaseResult selectDetail(Integer customerId) {
        return BaseResult.ok(heartVoiceMapper.selectNewOne(customerId));
    }

    public BaseResult selectHeartVoicePageGroup(Page page, Customer customer, Integer isAbnormal, String start, String end) {
        Integer stationId = stationService.getLoginUserStation();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Map> maps = heartVoiceMapper.selectHeartVoicePageGroup(customer, isAbnormal, start, end,stationId);
        PageInfo<Map> pageInfo = new PageInfo<>(maps);
        return BaseResult.ok(pageInfo);
    }

    public void deleteByCustomerIds(Integer[] ids) {
        int i = 0;
        if (ids.length > 0) {
            i = heartVoiceMapper.deleteByCustomerIds(ids);
        }
    }

    public BaseResult selectSmallPersonData(Page page, Integer customerId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        HeartVoiceExample heartVoiceExample = new HeartVoiceExample();
        heartVoiceExample.createCriteria().andCustomerIdEqualTo(customerId);
        heartVoiceExample.setOrderByClause("`create_time` DESC");
        List<HeartVoice> heartVoices = heartVoiceMapper.selectByExample(heartVoiceExample);
        return BaseResult.ok(new PageInfo<HeartVoice>(heartVoices));
    }

    public List<IndexGraphDayResDTO> selectGraphWithDay(IndexReqDTO indexReqDTO) {
        return heartVoiceMapper.selectGraphWithDay(indexReqDTO);
    }

    public List<IndexGraphResDTO> selectGraphData(IndexReqDTO indexReqDTO) {
        return heartVoiceMapper.selectGraphData(indexReqDTO);
    }

    public List<HeartVoice> selectCustomerHeartVoice(String customerId, Integer scope) {
        return heartVoiceMapper.selectCustomerHeartVoice(customerId,scope);
    }
}
